<?php

/**
 * Class for easier work with html forms
 * @author st32417
 */
class Form {

    private $fText = "";       // form html
    private $pStarted = false; // if the <p> tag started

    /**
     * Beggins the form with given params.
     * @param string $action
     * @param type $method
     * @param type $id
     * @param type $attr_ar
     * @return \Form
     */
    public function startForm($action = '#', $method = 'post', $id = NULL, $attr_ar = array()) {
        if ($action === 'self'){
            $action = $_SERVER['SCRIPT_NAME'] . '?' . $_SERVER['QUERY_STRING'];
        }
        $str = "<form action=\"$action\" method=\"$method\"";
        if (isset($id)) {
            $str .= " id=\"$id\"";
        }
        $str .= $attr_ar ? $this->addAttributes($attr_ar) . '>' : ">";
        $this->fText = $str;
        return $this;
    }

    /**
     * 
     * @param type $attr_ar
     * @return type
     */
    private function addAttributes($attr_ar = array()) {
        $str = '';
        $min_atts = array('checked', 'disabled', 'readonly', 'multiple');
        foreach ($attr_ar as $key => $val) {
            if (in_array($key, $min_atts)) {
                if (!empty($val)) {
                    $str .= " $key=\"$key\"";
                }
            } else {
                $str .= " $key=\"$val\"";
            }
        }
        return $str;
    }

    /**
     * Adds input element to actual form.
     * @param type $type
     * @param type $name
     * @param type $value
     * @param type $attr_ar
     * @return \Form
     */
    public function addInput($type, $name, $value = '', $attr_ar = array()) {
        $str = "";
        if (!$this->pStarted){
            $str = "<p>";
            $this->pStarted = true;
        }
        $str .= "<input type=\"$type\" id=\"$name\" name=\"$name\" value=\"$value\"";
        if ($attr_ar) {
            $str .= $this->addAttributes($attr_ar);
        }
        $str .= ' />';
        if ($this->pStarted){
            $str .= '</p>';
            $this->pStarted = false;
        }
        $this->fText .= $str;
        return $this;
    }
    
    /**
     * Adds h3 title to the form.
     * @param type $text
     */
    public function addDescription($text){
        $this->fText .= "<h3>". $text . "</h3>";
    }

    /**
     * Add textarea to actual form.
     * @param type $name
     * @param type $value
     * @param type $attr_ar
     * @return \Form
     */
    public function addTextArea($name, $value = '', $attr_ar = array()) {
        $str = "";
        if (!$this->pStarted){
            $str .= "<p>";
            $this->pStarted = true;
        }
        $str .= "<textarea name=\"$name\"";
        if ($attr_ar) {
            $str .= $this->addAttributes($attr_ar);
        }
        $str .= ">$value</textarea></p>";
        $this->fText .= $str;
        return $this;
    }
    
    /**
     * Adds selection box to actual form.
     * @param type $name
     * @param type $data - array returned by db::sql('select  ... ') function
     * @param type $colName - name of column to show in selection box
     * @param type $refName - name of column to be a value of selection box
     * @param type $selValue - selected value
     * @param type $size - 1... selection box, 2 and more ... list
     * @param type $multiple - true... multi select
     * @param type $attr_ar
     * @return
     */
    public function addSelectionBox($name, $data, $colName, $refName, $selValue = null, $size = 1, $multiple=false, $attr_ar = array()) {
        $str = '';
        if (!$this->pStarted){
            $str .= "<p>";
            $this->pStarted = true;
        }
        $bracket = $multiple ? '[]' : '';
        $str .= "<select name=\"$name". $bracket ."\" id=\"$name\" size=\"$size\"";
        
        if ($attr_ar) {
            $str .= $this->addAttributes($attr_ar);
        }
        if ($multiple && $size>1){
            $str .= ' multiple';
        }
        $str .= ">";
        
        foreach ($data as $row) {
            $str .= "<option value=\"". $row[$refName] ."\"";
            if ($selValue){
                if ($multiple){
                    if (in_array($row[$refName], $selValue)) {
                        $str .= ' selected';
                    }
                } else {
                    if ($selValue == $row[$refName]) {
                        $str .= ' selected';
                    }
                }
            }
            $str .= '>' . $row[$colName] . '</option>';
        }
        
        $str .= "</select></p>";
        $this->fText .= $str;
        return $this;
    }

    /**
     * Adds label to actual form.
     * @param type $text
     * @param type $for
     * @param type $attr_ar
     * @return \Form
     */
    public function addLabel($text, $for, $attr_ar = array()) {
        $str = "<p><label ";
        $this->pStarted = true;
        if (!empty($for)) {
            $str .= " for=\"$for\"";
        }
        if ($attr_ar) {
            $str .= $this->addAttributes($attr_ar);
        }
        $str .= ">$text</label>";
        $this->fText .= $str;
        return $this;
    }

    /**
     * Ends actual form.
     * @return \Form
     */
    public function endForm() {
        $str = "</form>";
        $this->fText .= $str;
        return $this;
    }
    
    /**
     * Call after endForm() method!!
     * @return html text representing form
     */
    public function getForm(){
        return $this->fText;
    }

}

?>
